<?php include "header.php"; ?>
	<script type = "text/javascript">
		function change() {
			document.getElementById("addCar").submit();
		}
	</script>
	<?php 
		if (isset($_POST["make"]))
			$make = $_POST["make"];
		else
			$make = "Select Make";
		
		if (isset($_POST["model"]))
			$model = $_POST["model"];
		else
			$model = "Select Model";

		if(isset($_POST["year"]))
			$year = $_POST["year"];
		else
			$year = "Select Year";



		echo '<form id="addCar" method="POST" action="" onchange="change()">
			Make: <select name="make"><option value="' . $make . '">' . $make . '</option>';
		$query = "SELECT make FROM car";
		if ($result = mysqli_query($con, $query)) {
			if (mysqli_num_rows($result) > 0) {
				while ($row = mysqli_fetch_array($result)) {
					echo '<option value="' . $row["make"] . '">' . $row["make"] . '</option>';
				}
			}
		}
		echo '</select>
			Model: <select name="model"><option value="' . $model . '">' . $model . '</option>';
		$query = "SELECT model FROM car WHERE make = '" . $make . "'";
		if ($result = mysqli_query($con, $query)) {
			if (mysqli_num_rows($result) > 0) {
				while ($row = mysqli_fetch_array($result)) {
					echo '<option value="' . $row["model"] . '">' . $row["model"] . '</option>';
				}
			}
		}

		echo '</select>
			Year: <select name="year"><option value="' . $year . '">' . $year . '</option>';
		$query = "SELECT year FROM car WHERE make = '" . $make . "' AND model = '" . $model . "'";
		if ($result = mysqli_query($con, $query)) {
			if (mysqli_num_rows($result) > 0) {
				while ($row = mysqli_fetch_array($result)) {
					echo '<option value="' . $row["year"] . '">' . $row["year"] . '</option>';
				}
			}
		}
		echo '</select> </form>';
	?>
<?php include "footer.php"; ?>